<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>调试菜单树</title>
    <style>
        body { font-family: monospace; padding: 20px; }
        pre { background: #f5f5f5; padding: 10px; border-radius: 5px; }
        .tree { margin-left: 20px; }
    </style>
</head>
<body>
    <h1>调试菜单树结构</h1>
    
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    require __DIR__ . '/../vendor/autoload.php';
    
    try {
        $app = new think\App();
        $app->initialize();
        
        echo "<h2>1. 获取菜单树</h2>";
        
        $menus = \think\admin\service\MenuService::getTree();
        
        echo "<p>返回的一级菜单数量: <strong>" . count($menus) . "</strong></p>";
        
        echo "<h3>菜单树结构：</h3>";
        echo "<pre>";
        
        function printMenu($menus, $level = 0) {
            $indent = str_repeat('  ', $level);
            foreach ($menus as $menu) {
                echo $indent . "├─ {$menu['title']} (ID:{$menu['id']}, URL:{$menu['url']})\n";
                
                if (!empty($menu['sub'])) {
                    echo $indent . "│  子菜单数量: " . count($menu['sub']) . "\n";
                    printMenu($menu['sub'], $level + 1);
                }
            }
        }
        
        printMenu($menus);
        
        echo "</pre>";
        
        echo "<h2>2. 检查权限管理菜单</h2>";
        
        $authMenu = null;
        foreach ($menus as $menu) {
            if ($menu['id'] == 9 || $menu['title'] === '权限管理') {
                $authMenu = $menu;
                break;
            }
        }
        
        if ($authMenu) {
            echo "<p style='color:green'>✓ 找到权限管理菜单</p>";
            echo "<pre>";
            echo "ID: {$authMenu['id']}\n";
            echo "标题: {$authMenu['title']}\n";
            echo "URL: {$authMenu['url']}\n";
            echo "子菜单数量: " . (empty($authMenu['sub']) ? 0 : count($authMenu['sub'])) . "\n";
            
            if (!empty($authMenu['sub'])) {
                echo "\n子菜单：\n";
                foreach ($authMenu['sub'] as $sub) {
                    echo "  ├─ {$sub['title']} (ID:{$sub['id']}, URL:{$sub['url']})\n";
                    
                    if (!empty($sub['sub'])) {
                        echo "     三级菜单数量: " . count($sub['sub']) . "\n";
                        foreach ($sub['sub'] as $subsub) {
                            echo "     │  └─ {$subsub['title']} (ID:{$subsub['id']}, URL:{$subsub['url']})\n";
                        }
                    }
                }
            }
            echo "</pre>";
            
            // 判断应该显示在哪里
            $hasLevel3 = false;
            if (!empty($authMenu['sub'])) {
                foreach ($authMenu['sub'] as $sub) {
                    if (!empty($sub['sub'])) {
                        $hasLevel3 = true;
                        break;
                    }
                }
            }
            
            if ($hasLevel3) {
                echo "<p style='color:green; font-size:18px'><strong>✓ 权限管理有三级菜单，应该显示在【左侧】</strong></p>";
            } else {
                echo "<p style='color:red; font-size:18px'><strong>❌ 权限管理只有二级菜单，会显示在【顶部】</strong></p>";
                echo "<p>这就是问题所在！</p>";
            }
            
        } else {
            echo "<p style='color:red'>❌ 没有找到权限管理菜单！被过滤掉了</p>";
        }
        
        echo "<h2>3. 完整的菜单数据（JSON）</h2>";
        echo "<pre>";
        echo json_encode($menus, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
        echo "</pre>";
        
    } catch (Exception $e) {
        echo "<p style='color:red'>错误: " . $e->getMessage() . "</p>";
        echo "<pre>" . $e->getTraceAsString() . "</pre>";
    }
    ?>
    
</body>
</html>

